package com.oa.business.acc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.business.acc.dto.AccountDto;
import com.oa.business.acc.dto.PrincipalDto;
import com.oa.common.result.Result;

import com.oa.framework.acc.account.domain.UserAccount;
import com.oa.framework.acc.account.service.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author cuichao
 */
@RestController
@RequestMapping("/acc")
public class AccController {

    @Autowired
    private UserAccountService accountService;

    @GetMapping("search")
    public Result search(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        Page<UserAccount> pageWrapper = new Page(page, limit);
        IPage<UserAccount> users = accountService.searchAccountIncludePriorityPrincipal(pageWrapper);
        return Result.paginationResult(users.getPages(), users.getSize(), users.getTotal(), users.getRecords());
    }

    @RequestMapping("info/{id}")
    public Result info(@PathVariable Long id) {
        return Result.success(accountService.getUserAccountIncludePrincipalsByAccountId(id));
    }


    @PostMapping("/add")
    public Result add(@RequestBody AccountDto dto) {
        return Result.success(accountService.createUserAccount(dto.transform()));

    }

    @PostMapping("/edit")
    public Result edit(@RequestBody AccountDto dto) {
        return Result.success(accountService.updateUserAccount(dto.transform()));
    }

    @PostMapping("/{accountId}/principal/add")
    public Result addPrincipal(@PathVariable Long accountId, @RequestBody PrincipalDto dto) {
        return Result.success(accountService.addUserAccountPrincipal(accountId,dto.transform()));
    }

    @PostMapping("/{accountId}/principal/edit")
    public Result editPrincipal(@PathVariable Long accountId, @RequestBody PrincipalDto dto){
        return Result.success(accountService.updateUserAccountPrincipal(accountId,dto.transform()));
    }


    @PostMapping("/{accountId}/principal/remove/{principalId}")
    public Result removePrincipal(@PathVariable("accountId") Long accountId, @PathVariable("principalId") Long principalId) {
        return Result.success(accountService.removeAccountPrincipal(accountId, principalId));
    }
}
